package com.loong.musicdemo.widget.iconbtn;


import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.loong.musicdemo.LoginActivity;
import com.loong.musicdemo.R;
import com.loong.musicdemo.util.enumutil.LoginClickType;
import com.loong.musicdemo.widget.CustomDialog.CommonDialog;

import java.util.Objects;

/**
 * 自定义图标按钮
 *
 * @author longchao
 */
public class IconButton extends LinearLayout {
    private final ImageButton icon;//图标
    private final TextView name;//登录方式名

    public IconButton(Context context) {
        super(context);//使用布局资源填充视图
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Objects.requireNonNull(inflater).inflate(R.layout.widget_icon_button, this, true);
        //加载布局文件
        this.icon = findViewById(R.id.icon);
        this.name = findViewById(R.id.name);
    }
    public IconButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //使用布局资源填充视图
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Objects.requireNonNull(inflater).inflate(R.layout.widget_icon_button, this, true);//这里需要使用true，使得子视图也加入到视图容器中
        //加载布局文件
        this.icon = findViewById(R.id.icon);
        this.name = findViewById(R.id.name);
    }

    /**
     * 按钮点击事件
     *
     * @param context 当前活动上下文
     * @return 返回本类对象
     */
    public IconButton setOnClickListener(Context context, LoginClickType clickType) {
        this.icon.setOnClickListener(v -> {
            switch (clickType){
                case PHONE:
                case EMAIL:
                    ((LoginActivity)context).changeLoginType(clickType);
                    Toast.makeText(context, ("您已切换至"+clickType.getType()+"登录。"), Toast.LENGTH_SHORT).show();
                    break;
                case OPEN_APP:
                   CommonDialog dialog =  new CommonDialog(context);
                           dialog.setTitle("提示")
                            .setMessage("暂不支持该登录方式！")
                            .setPositive("确认")
                            .setNegative("取消")
                            .setOnclickBottomListener(new CommonDialog.OnclickBottomListener() {
                                @Override
                                public void onPositiveClick() {
                                    dialog.dismiss();
                                }

                                @Override
                                public void onNegativeClick() {

                                }
                            })
                            .show();
                    break;
                default:
                    Toast.makeText(context,"暂未开通此登录方式！",Toast.LENGTH_SHORT).show();
                    break;
            }
        });
        return this;
    }
    //内部接口实现按钮监听
    public interface OnclickBottomListener{
        //实现确认按钮
        void onClick();
    }
    private OnclickBottomListener onclickBottomListener;

    public IconButton setOnClickListener(OnclickBottomListener onclickBottomListener){
        this.onclickBottomListener = onclickBottomListener;//绑定监听事件
        //设置确认以及取消事件
        this.icon.setOnClickListener(v->{
            if (this.onclickBottomListener!=null){
                //绑定不为空时
                this.onclickBottomListener.onClick();
            }
        });
        return this;
    }
    public IconButton setTextColor(@ColorInt int color){
        this.name.setTextColor(color);
        return this;
    }
    public IconButton setName(String name) {
        this.name.setText(name);
        return this;
    }

    public IconButton setIcon(int id) {
        this.icon.setImageResource(id);
        return this;
    }
}
